package com.surfilter.massdata.spark.util;

import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 系统配置工具
 * 加载系统配置文件
 */
public class SysProps {
	private static final String CONF_FILE = "/config.properties";

	private static final Log log = LogFactory.getLog(SysProps.class);

	private Properties props = null;
	
	private SysProps(){
		props = new Properties();
		try {
			props.load(SysProps.class.getResourceAsStream(CONF_FILE));
		} catch (IOException e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(), e);
			}
		}
	}
	
	private static final SysProps instance = new SysProps();
	
	public static SysProps getInstance(){
		return instance;
	}
	
	/**
	 * 取得系统配置项
	 * @param key
	 * @return
	 */
	
	public String getValue(String key){
		return StringUtils.trim(props.getProperty(key));
	}

	public String getValue(String key,String defaultValue){
		String value = getValue(key);
		if(StringUtils.isNotBlank(value)){
			return value;
		}else{
			return defaultValue;
		}
	}
	
	public int getInteger(String key,int defaultValue){
		int returnValue = 0;
		try{
			String value = getValue(key);
			
			if(StringUtils.isNotBlank(value)){
				returnValue = Integer.parseInt(value);
			}else{
				returnValue = defaultValue;
			}
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return returnValue;
	}
}


